package cn.nku.shichangwei;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TakesDao extends BaseDao{
	public boolean addTakes(Takes tak){
		String sql="insert into takes values(?,?)";
		try {
			java.sql.PreparedStatement pre=con.prepareStatement(sql);
			pre.setString(1,tak.getStuid());
			pre.setString(2,tak.getCouid());
			if(pre.executeUpdate()>0)
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
	}
	public List<Takes> selectTakes(Takes t) {
		// TODO Auto-generated method stub
		String sql="select * from select_takes ";
		List<Takes> retList=new ArrayList<>();
		if(t.getStuid()!=null&&t.getStuid().length()!=0){
			sql+="where stuid="+t.getStuid();
		}
		java.sql.PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			ResultSet executeQuery=pre.executeQuery();
			while(executeQuery.next()){
				Takes ta=new Takes();
				ta.setStuid(executeQuery.getString("stuid"));
				ta.setCouid(executeQuery.getString("couid"));
				ta.setStuname(executeQuery.getString("stuname"));
				ta.setCouname(executeQuery.getString("couname"));
				retList.add(ta);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retList;
	}

	public boolean update(Takes t) {
		// TODO Auto-generated method stub
		String sql = "update takes set couid=? where stuid=?";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setString(1, t.getCouid());
			preparedStatement.setString(2, t.getStuid());
			if(preparedStatement.executeUpdate() > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public boolean delete(String stuid,String couid) {
		// TODO Auto-generated method stub
		String sql="delete from takes where stuid=? and couid=?";
		java.sql.PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, stuid);
			pre.setString(2, couid);
			if(pre.executeUpdate()>0)
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
